package org.gnomus.data;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;

import java.util.HashMap;

public class object extends HashMap<String, Object> {
    public Key key = null;

    public object() {
    }

    public object(Entity e) {
        key = e.getKey();
        setKind(e.getKind());
        setId((String) e.getProperty("id"));
        setCreated((Long) e.getProperty("created"));
    }

    public object(String kind) {
        setKind(kind);
    }

    public void save() {
        Transaction txn = store.beginTransaction();
        try {
            key = store.put(getKind(), this);
            txn.commit();
        } finally {
            Entity e = store.get(key);
            if (txn.isActive()) {
                txn.rollback();
            } else if (e != null) {
                setId((String) e.getProperty("id"));
                setCreated((Long) e.getProperty("created"));
            } else {
                // TODO : SOMETHING WENT WRONG
            }
        }
    }

    public void setKind(String kind) {
        put("kind", kind);
    }

    public String getKind() {
        return (String) get("kind");
    }

    public void setId(String id) {
        put("id", id);
    }

    public String getId() {
        return (String) get("id");
    }

    public void setCreated(Long created) {
        put("created", created);
    }

    public Long getCreated() {
        return (Long) get("created");
    }
}
